<?php
class Admin_SeniorController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;

        $tSMO = new SeniorModel;
        $tWMO = new WorkerModel;
        $tAMO = new AdminModel;
        $tSIMO = new SexinfoModel;
        $tNIMO = new NationinfoModel;
        $tRIMO = new ReligioninfoModel;
        $tSWMO = new SupportworkerModel;
        $tWSMO = new WorkerseniorModel;
        $tDIMO = new DepartmentinfoModel;
        $tBIMO = new BodystatusinfoModel;
        $tFMMO = new FamilymembersModel;

        $tWhere = ' 1 and s.status = 1 and fm.is_client = 1 and s.leave_status = 2';

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and s.status = 1 and fm.is_client = 1 and s.leave_status = 2 and s.department = '.$tAData[0]['department'];
            }
        }
        $pSeniorname = empty($p['senior_name'])?'':Tool_Fnc::safe_string($p['senior_name']);
        $pCreated = empty($p['created'])?'':Tool_Fnc::safe_string($p['created']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        $pName = empty($p['name'])?'':Tool_Fnc::safe_string($p['name']);
        $pPhonenumber = empty($p['phonenumber'])?'':Tool_Fnc::safe_string($p['phonenumber']);

        if(!empty($pName)){
            $tWhere .= ' and s.name like \'%'.$pName.'%\'';
        }
        if(!empty($pCreated)){
            $tWhere .= ' and FROM_UNIXTIME(s.created,\'%Y-%m-%d\') = \''.$pCreated.'\'';
        }
        if(!empty($pPhonenumber)){
            $tWhere .= ' and fm.phone like \'%'.$pPhonenumber.'%\'';
        }
        if(!empty($pDepartment)){
            $tWhere .= ' and s.department = '.$pDepartment;
        }
        
        //$tCount = $tSMO->field('count(*) c')->where($tWhere)->fList();
        //$tSql = 'select count(*) c from '.$tSMO->table.' s where '.$tWhere;
        $tSql = 'select count(*) c from '.$tSMO->table.' s left join '.$tFMMO->table.' fm on s.id = fm.sid where '.$tWhere;
        $tCount = $tSMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        $tDatas = array();
        if(!empty($tCnt)){
                /*$tSql = 'select * from '.$tSMO->table.' where '.$tWhere.' order by id desc limit '.$tPage->limit();             echo $tSql;*/   
            $tSql = 'select w.name w_name,ni.introduce nation,si.introduce sex,di.introduce department,s.id id,s.name name,s.age age,s.religion religion,bi.introduce body_status,s.created created,sw.name sw_name from '.$tSMO->table.' s left join '.$tWSMO->table.' ws on ws.sid = s.id left join '.$tSWMO->table.' sw on sw.id = ws.sw_id left join '.$tDIMO->table.' di on s.department = di.num left join '.$tBIMO->table.' bi on s.body_status = bi.num left join '.$tSIMO->table.' si on s.sex = si.num left join '.$tNIMO->table.' ni on ni.num = s.nation left join '.$tAMO->table.' a on a.id = ws.sw_id left join '.$tWMO->table.' w on w.id = a.w_id left join '.$tFMMO->table.' fm on fm.sid = s.id where '.$tWhere.' order by id desc limit '.$tPage->limit();
            $tDatas = $tSMO->query($tSql);
            foreach($tDatas as $tK=>$tV){
                $tFData = $tFMMO->field('name,phone')->where('sid = '.$tV['id'].' and is_client = 1')->fRow();
                $tDatas[$tK]['wt_name'] = $tFData['name'];
                $tDatas[$tK]['wt_phone'] = $tFData['phone'];
            }
            //$tDatas = $tSMO->field('*')->where($tWhere)->order(' id desc')->limit($tPage->limit())->fList();
        }

        //所在部门展示
        $tDIDatas = $tDIMO->field('num,introduce')->fList();

        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('pName',$pName);
        $this->assign('pDepartment',$pDepartment);
        $this->assign('pPhonenumber',$pPhonenumber);
        $this->assign('pCreated',$pCreated);
        $this->assign('tShow',$tPage->show());
        $this->assign('tDatas',$tDatas);
        $this->assign('getSex',$tSIMO->tSexinfo);
        $this->assign('getNation',$tNIMO->tNationinfo);
        $this->assign('getReligion',$tRIMO->tReligioninfo);
        $this->assign('getBodystatus',$tSMO->tBodystatus);
    }
    public function addAction(){
        $p = $_REQUEST;
            $tSMO = new SeniorModel;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pSeniorname = empty($p['senior_name'])?Tool_Fnc::ajaxMsg('姓名不能为空'):Tool_Fnc::safe_string($p['senior_name']);
            $pSex = empty($p['sex'])?'':Tool_Fnc::safe_string($p['sex']);
            $pAge = empty($p['age'])?Tool_Fnc::ajaxMsg('年龄不能为空'):Tool_Fnc::safe_string($p['age']);
            $pNation = empty($p['nation'])?'':Tool_Fnc::safe_string($p['nation']);
            $pReligion = empty($p['religion'])?'':Tool_Fnc::safe_string($p['religion']);
            $pPermanentresidence = empty($p['permanent_residence'])?'':Tool_Fnc::safe_string($p['permanent_residence']);
            $pCritcode = empty($p['critcode'])?Tool_Fnc::ajaxMsg('身份证号不能为空'):Tool_Fnc::safe_string($p['critcode']);
            $pBirthdate = empty($p['birth_date'])?Tool_Fnc::ajaxMsg('出生年月不能为空'):Tool_Fnc::safe_string($p['birth_date']);
            $pMarried = empty($p['married'])?'':Tool_Fnc::safe_string($p['married']);
            $pCardnumber = empty($p['card_number'])?'':Tool_Fnc::safe_string($p['card_number']);
            $pBasicfee = empty($p['basic_fee'])?'':Tool_Fnc::safe_string($p['basic_fee']);
            $pChargediscount = empty($p['charge_discount'])?'':Tool_Fnc::safe_string($p['charge_discount']);
            $pPayment = empty($p['payment'])?'':Tool_Fnc::safe_string($p['payment']);
            $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
            $pEducationlevels = empty($p['education_levels'])?'':Tool_Fnc::safe_string($p['education_levels']);
            $pBodystatus = empty($p['body_status'])?'':Tool_Fnc::safe_string($p['body_status']);
            $pMedicalstatus = empty($p['medical_status'])?'':Tool_Fnc::safe_string($p['medical_status']);
            $pOldaddress = empty($p['old_address'])?'':Tool_Fnc::safe_string($p['old_address']);
            $pOldcompany = empty($p['old_company'])?'':Tool_Fnc::safe_string($p['old_company']);
            $pTemperhobby = empty($p['temper_hobby'])?'':Tool_Fnc::safe_string($p['temper_hobby']);
            $pAllergies = empty($p['allergies'])?Tool_Fnc::ajaxMsg('病史过敏不能为空'):Tool_Fnc::safe_string($p['allergies']);
            //委托人信息
            $pWtname = empty($p['wt_name'])?'':Tool_Fnc::safe_string($p['wt_name']);
            $pWtrelation = empty($p['wt_relation'])?'':Tool_Fnc::safe_string($p['wt_relation']);
            $pWtphone = empty($p['wt_phone'])?'':Tool_Fnc::safe_string($p['wt_phone']);
            $pWtaddress = empty($p['wt_address'])?'':Tool_Fnc::safe_string($p['wt_address']);
            $pWtcritcode = empty($p['wt_critcode'])?'':Tool_Fnc::safe_string($p['wt_critcode']);
            $pDiet = empty($p['diet'])?'':Tool_Fnc::safe_string($p['diet']);
            $pLife = empty($p['life'])?'':Tool_Fnc::safe_string($p['life']);
            $pSleep = empty($p['sleep'])?'':Tool_Fnc::safe_string($p['sleep']);
            $pMedicalorders = empty($p['medical_orders'])?'':Tool_Fnc::safe_string($p['medical_orders']);
            $pPhysicalexamination = empty($p['physical_examination'])?'':Tool_Fnc::safe_string($p['physical_examination']);
            $pDischargediagnosis = empty($p['discharge_diagnosis'])?'':Tool_Fnc::safe_string($p['discharge_diagnosis']);
            $pGoods = empty($p['goods'])?'':Tool_Fnc::safe_string($p['goods']);
            $pIstalk = empty($p['is_talk'])?'':Tool_Fnc::safe_string($p['is_talk']);
            //家属信息
            $pJsname = empty($p['js_name'])?'':$p['js_name'];
            $pJssex = empty($p['js_sex'])?'':$p['js_sex'];
            $pJsrelation = empty($p['js_relation'])?'':$p['js_relation'];
            $pJscritcode = empty($p['js_critcode'])?'':$p['js_critcode'];
            $pJsphone = empty($p['js_phone'])?'':$p['js_phone'];
            $pJsaddress = empty($p['js_address'])?'':$p['js_address'];
            $tLength = count($pJsname);
            $tJs = array(array());
			$pBirthdate = str_replace('-','',$pBirthdate);
            for($i=0;$i<$tLength;$i++){
                $tJs[$i]['name'] = $pJsname[$i];
                $tJs[$i]['sex'] = $pJssex[$i];
                $tJs[$i]['relation'] = $pJsrelation[$i];
                $tJs[$i]['critcode'] = $pJscritcode[$i];
                $tJs[$i]['phone'] = $pJsphone[$i];
                $tJs[$i]['address'] = $pJsaddress[$i];
            }
            $tTime = time();
            if(!empty($_FILES['img'])){
                //if($_FILES['photo']['size'] > 500*1024){Tool_Fnc::ajaxMsg('图片过大，请选择小于500KB的图片重新上传');}
                $tImgurl = '/upload/senior/'.date('Y').'/'.date('m').'/'.date('d').'/';
                $tDir = APPLICATION_PATH . '/public'.$tImgurl;
                $tUpload = new Tool_Upload($_FILES['img'],$tDir);
                $tSavename = $tUpload->getSaveName();
                $tExt = $tUpload->extension;
                $tFile = $tSavename . '.' . $tExt;
                $res = $tUpload->upload($tSavename);
                if($res == 1){
                    $tHeadimg = $tImgurl . $tFile;
                }else{
                    Tool_Fnc::ajaxMsg('照片上传失败');
                }
            } 
            //基本信息
            $tData = array(
                'name' => $pSeniorname,
                'photo' => $tHeadimg,
                'sex' => $pSex,
                'age' => $pAge,
                'nation' => $pNation,
                'religion' => $pReligion,
                'permanent_residence' => $pPermanentresidence,
                'critcode' => $pCritcode,
                'birth_date' => $pBirthdate,
                'married_status' => $pMarried,
                'department' => $pDepartment,
                'education_levels' => $pEducationlevels,
                'body_status' => $pBodystatus,
                'medical_status' => $pMedicalstatus,
                'old_address' => $pOldaddress,
                'old_company' => $pOldcompany,
                'temper_hobby' => $pTemperhobby,
                'allergies' => $pAllergies,
                'diet' => $pDiet,
                'life' => $pLife,
                'sleep' => $pSleep,
                'medical_orders' => $pMedicalorders,
                'physical_examination' => $pPhysicalexamination,
                'discharge_diagnosis' => $pDischargediagnosis,
                'goods' => $pGoods,
                'status' => 1,
                'created' => $tTime,
            );
            $tSMO->begin();
            $tSData = $tSMO->field('count(id) c,id')->where('critcode = "'.$pCritcode.'"')->fList();

            if(!empty($tSData[0]['c'])){Tool_Fnc::ajaxMsg('该老人已经存在！');}

            if(empty($tSData[0]['c'])){
                if(!$tId = $tSMO->insert($tData)){
                    $tSMO->back();
                    Tool_Fnc::ajaxMsg('添加失败 001');
                }
            }
            //医保信息
            $tData = array(
                'card_number' => $pCardnumber,
                'basic_fee' => $pBasicfee,
                'charge_discount' => $pChargediscount,
                'payment' => $pPayment,
                'sid' => $tId,
                'created' => $tTime,
            ); 
            $tMIMO = new MedicalinsuranceModel;
            if(empty($tSData[0]['c'])){
                if(!$tMIMO->insert($tData)){
                    $tSMO->back();
                    Tool_Fnc::ajaxMsg('添加失败 002');
                }
            }
            //委托人信息
            $tData = array(
                'name' => $pWtname,
                'relation' => $pWtrelation,
                'phone' => $pWtphone,
                'address' => $pWtaddress,
                'critcode' => $pWtcritcode,
                'is_client' => 1,
                'is_talk' => $pIstalk,
                'created' => $tTime,
                'sid' => $tId,
            );
            $tFMMO = new FamilymembersModel;
            if(empty($tSData[0]['c'])){
                if(!$tFMMO->insert($tData)){
                    $tSMO->back();
                    Tool_Fnc::ajaxMsg('添加失败 003');
                }
            }

            if(empty(count($tJs))){Tool_Fnc::ajaxMsg('至少添加一位家属才可提交!');}

            if(!empty($tJs)){
            //家属信息
                foreach($tJs as $tJsrow){
                    $tData = array(
                        'name' => $tJsrow['name'],
                        'sex' => $tJsrow['sex'],
                        'relation' => $tJsrow['relation'],
                        'critcode' => $tJsrow['critcode'],
                        'phone' => $tJsrow['phone'],
                        'address' => $tJsrow['address'],
                        'is_client' => 2,
                        'status' => 1,
                        'created' => $tTime,
                        'sid' => $tId,
                    );
                    if(empty($tSData[0]['c'])){
                        if(!$tFMMO->insert($tData)){
                            $tSMO->back();
                            Tool_Fnc::ajaxMsg('添加失败 004');
                        }
                    }
                }
            }
            #个性化初始设置
            $tPMO = new PlanModel;
            $tPIMO = new PlaninfoModel;  
            $tEIMO = new EventinfoModel;
            //$tPIDatas = $tPIMO->field('id,name,descriptions,at')->fList();
            $tSql = 'select pi.id id,ei.num event_type,pi.descriptions descriptions,pi.at at from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on pi.event_type = ei.num order by pi.id asc ';
            $tPIDatas = $tPIMO->query($tSql);
            if(empty($tSData[0]['c'])){
            if(!empty($tPIDatas)){
                foreach($tPIDatas as $tK=>$tV){
                    $tData = array(
                        'sid' => $tId,
                        'pi_id' => $tV['id'],
                        'plan_type' => 1,
                        'event_type' => $tV['event_type'],
                        'status' => 1,
                        'starttime' => strtotime(date('Y-m-d',$tTime)),
                        'endtime' => strtotime(date('Y-m-d',$tTime)),
                        'respond_time' => '00:00:00',
                        'description' => $tV['descriptions'],
                        'at' => $tV['at'],
                        'is_show' => 1,
                        'created' => $tTime,
                    );
                    if(!$tPMO->insert($tData)){
                        Tool_Fnc::ajaxMsg('系统错误002');
                    }
                }
            }
            }
            //扫描件信息
            if(!empty($_FILES['file_img'])){
                $tScMO = new ScanningModel;

                $fileimg = $_FILES['file_img'];
                $tImgurl = '/upload/scanning/'.date('Y').'/'.date('m').'/'.date('d').'/';
                $tDir = APPLICATION_PATH.'/public/upload/scanning/'.date('Y').'/'.date('m').'/'.date('d').'/';
                if($this->createDir($tDir)){
                    for($i=0;$i<count($fileimg['name']);$i++){
		                $tSaveName = $this->getSaveName();
                        if(strstr($fileimg['type'][$i],'image/jpeg')!==''){  
                            $tExt = 'jpg';  
                        }elseif(strstr($fileimg['type'][$i],'image/gif')!==''){  
                            $tExt ='gif';  
                        }elseif(strstr($fileimg['type'][$i],'image/png')!==''){  
 			                $tExt ='png';  
                        }else{
                            Tool_Fnc::ajaxMsg('非法文件');exit;
                        } 
                        $name = $fileimg['name'][$i];
                        $tImg = $tDir.$tSaveName.'.'.$tExt;
                        if(!move_uploaded_file($fileimg['tmp_name'][$i],iconv('UTF-8','gb2312',$tImg))){
                            Tool_Fnc::ajaxMsg('上传失败');exit;
                        };
                        //扫描件入库
                        $tData = array(
                            'sid' => $tId,
                            'imgurl' => $tImgurl.$tSaveName.'.'.$tExt,
                            'created' => $tTime,
                        );
                        if(!$tScMO->insert($tData)){
                            Tool_Fnc::ajaxMsg('上传失败');exit;
                        }
                    }
                }
            }
            $tSMO->commit();
            Tool_Fnc::ajaxMsg('添加成功',1);
        }
        //信息显示
        #民族显示
        $tNIMO = new NationinfoModel;
        $tNIDatas = $tNIMO->field('num,introduce')->fList();
        #宗教显示
        $tRIMO = new ReligioninfoModel;
        $tRIDatas = $tRIMO->field('num,introduce')->fList();
        #所在部门显示
        $tDIMO = new DepartmentinfoModel;
        $tDIDatas = $tDIMO->field('num,introduce')->fList();
        #文化程度显示
        $tEIMO = new EducationlevelsinfoModel;
        $tEIDatas = $tEIMO->field('num,introduce')->fList();
        #身体状态显示
        $tBIMO = new BodystatusinfoModel;
        $tBIDatas = $tBIMO->field('num,introduce')->fList();
        //支付方式显示
        $tPIMO = new PaymentinfoModel;
        $tPIDatas = $tPIMO->field('num,introduce')->fList();
        //收费折扣显示
        $tDIMO = new DiscountinfoModel;
        $tDisDatas = $tDIMO->field('num,introduce')->fList();

        $this->assign('tNIDatas',$tNIDatas);
        $this->assign('tRIDatas',$tRIDatas);
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tEIDatas',$tEIDatas);
        $this->assign('tBIDatas',$tBIDatas);
        $this->assign('tPIDatas',$tPIDatas);
        $this->assign('tDisDatas',$tDisDatas);

    }
    public function editAction(){
        $p = $_REQUEST;
        $pSid = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);
        
        $tSMO = new SeniorModel;
        $tMIMO = new MedicalinsuranceModel;
        $tFMMO = new FamilymembersModel;
        $tScMO = new ScanningModel;
        
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pSid = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);
            $pSeniorname = empty($p['senior_name'])?Tool_Fnc::ajaxMsg('姓名不能为空'):Tool_Fnc::safe_string($p['senior_name']);
            $pSex = empty($p['sex'])?'':Tool_Fnc::safe_string($p['sex']);
            $pAge = empty($p['age'])?Tool_Fnc::ajaxMsg('年龄不能为空'):Tool_Fnc::safe_string($p['age']);
            $pNation = empty($p['nation'])?'':Tool_Fnc::safe_string($p['nation']);
            $pReligion = empty($p['religion'])?'':Tool_Fnc::safe_string($p['religion']);
            $pPermanentresidence = empty($p['permanent_residence'])?'':Tool_Fnc::safe_string($p['permanent_residence']);
            $pCritcode = empty($p['critcode'])?Tool_Fnc::ajaxMsg('身份证号不能为空'):Tool_Fnc::safe_string($p['critcode']);
            $pBirthdate = empty($p['birth_date'])?Tool_Fnc::ajaxMsg('出生年月不能为空'):Tool_Fnc::safe_string($p['birth_date']);
            $pMarried = empty($p['married'])?'':Tool_Fnc::safe_string($p['married']);
            $pCardnumber = empty($p['card_number'])?'':Tool_Fnc::safe_string($p['card_number']);
            $pBasicfee = empty($p['basic_fee'])?'':Tool_Fnc::safe_string($p['basic_fee']);
            $pChargediscount = empty($p['charge_discount'])?'':Tool_Fnc::safe_string($p['charge_discount']);
            $pPayment = empty($p['payment'])?'':Tool_Fnc::safe_string($p['payment']);
            $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
            $pEducationlevels = empty($p['education_levels'])?'':Tool_Fnc::safe_string($p['education_levels']);
            $pBodystatus = empty($p['body_status'])?'':Tool_Fnc::safe_string($p['body_status']);
            $pMedicalstatus = empty($p['medical_status'])?'':Tool_Fnc::safe_string($p['medical_status']);
            $pOldaddress = empty($p['old_address'])?'':Tool_Fnc::safe_string($p['old_address']);
            $pOldcompany = empty($p['old_company'])?'':Tool_Fnc::safe_string($p['old_company']);
            $pTemperhobby = empty($p['temper_hobby'])?'':Tool_Fnc::safe_string($p['temper_hobby']);
            $pAllergies = empty($p['allergies'])?Tool_Fnc::ajaxMsg('病史过敏不能为空'):Tool_Fnc::safe_string($p['allergies']);
            //委托人信息
            $pWtname = empty($p['wt_name'])?'':Tool_Fnc::safe_string($p['wt_name']);
            $pWtrelation = empty($p['wt_relation'])?'':Tool_Fnc::safe_string($p['wt_relation']);
            $pWtphone = empty($p['wt_phone'])?'':Tool_Fnc::safe_string($p['wt_phone']);
            $pWtaddress = empty($p['wt_address'])?'':Tool_Fnc::safe_string($p['wt_address']);
            $pWtcritcode = empty($p['wt_critcode'])?'':Tool_Fnc::safe_string($p['wt_critcode']);
            $pDiet = empty($p['diet'])?'':Tool_Fnc::safe_string($p['diet']);
            $pLife = empty($p['life'])?'':Tool_Fnc::safe_string($p['life']);
            $pSleep = empty($p['sleep'])?'':Tool_Fnc::safe_string($p['sleep']);
            $pMedicalorders = empty($p['medical_orders'])?'':Tool_Fnc::safe_string($p['medical_orders']);
            $pPhysicalexamination = empty($p['physical_examination'])?'':Tool_Fnc::safe_string($p['physical_examination']);
            $pDischargediagnosis = empty($p['discharge_diagnosis'])?'':Tool_Fnc::safe_string($p['discharge_diagnosis']);
            $pGoods = empty($p['goods'])?'':Tool_Fnc::safe_string($p['goods']);
            $pIstalk = empty($p['is_talk'])?'':Tool_Fnc::safe_string($p['is_talk']);
            //家属信息
            $pJsname = empty($p['js_name'])?'':$p['js_name'];
            $pJssex = empty($p['js_sex'])?'':$p['js_sex'];
            $pJsrelation = empty($p['js_relation'])?'':$p['js_relation'];
            $pJscritcode = empty($p['js_critcode'])?'':$p['js_critcode'];
            $pJsphone = empty($p['js_phone'])?'':$p['js_phone'];
            $pJsaddress = empty($p['js_address'])?'':$p['js_address'];
            $pJsid = empty($p['js_id'])?'':$p['js_id'];
            $tLength = count($pJsname);
            $tJs = array(array());
            for($i=0;$i<$tLength;$i++){
                $tJs[$i]['name'] = $pJsname[$i];
                $tJs[$i]['sex'] = $pJssex[$i];
                $tJs[$i]['relation'] = $pJsrelation[$i];
                $tJs[$i]['critcode'] = $pJscritcode[$i];
                $tJs[$i]['phone'] = $pJsphone[$i];
                $tJs[$i]['address'] = $pJsaddress[$i];
                $tJs[$i]['js_id'] = $pJsid[$i];
            }
            $tTime = time();
            if(!empty($_FILES['img'])){
                if($_FILES['photo']['size'] > 500*1024){Tool_Fnc::ajaxMsg('图片过大，请选择小于500KB的图片重新上传');}
                $tImgurl = '/upload/senior/'.date('Y').'/'.date('m').'/'.date('d').'/';
                $tDir = APPLICATION_PATH . '/public'.$tImgurl;
                $tUpload = new Tool_Upload($_FILES['img'],$tDir);
                $tSavename = $tUpload->getSaveName();
                $tExt = $tUpload->extension;
                $tFile = $tSavename . '.' . $tExt;
                $res = $tUpload->upload($tSavename);
                if($res == 1){
                    $tHeadimg = $tImgurl . $tFile;
                }else{
                    Tool_Fnc::ajaxMsg('照片上传失败');
                }
            }else{
                $tHeadimg = $p['img_view'];
            }
			$pBirthdate = str_replace('-','',$pBirthdate);

            //基本信息
            $tData = array(
                'name' => $pSeniorname,
                'photo' => $tHeadimg,
                'sex' => $pSex,
                'age' => $pAge,
                'nation' => $pNation,
                'religion' => $pReligion,
                'permanent_residence' => $pPermanentresidence,
                'critcode' => $pCritcode,
                'birth_date' => $pBirthdate,
                'married_status' => $pMarried,
                'department' => $pDepartment,
                'education_levels' => $pEducationlevels,
                'body_status' => $pBodystatus,
                'medical_status' => $pMedicalstatus,
                'old_address' => $pOldaddress,
                'old_company' => $pOldcompany,
                'temper_hobby' => $pTemperhobby,
                'allergies' => $pAllergies,
                'diet' => $pDiet,
                'life' => $pLife,
                'sleep' => $pSleep,
                'medical_orders' => $pMedicalorders,
                'physical_examination' => $pPhysicalexamination,
                'discharge_diagnosis' => $pDischargediagnosis,
                'goods' => $pGoods,
                'is_diabetes' => $pIsdiabetes,
                'status' => 1,
                'created' => $tTime,
                'id' => $pSid,
            );
            $tSMO->begin();
            if(!$tSMO->update($tData)){
                $tSMO->back();
                Tool_Fnc::ajaxMsg('修改失败 001');
            }
            $tMData = $tMIMO->field('id')->where('sid = '.$pSid)->fRow();
            if(!empty($tMData)){
                //医保信息
                $tData = array(
                    'card_number' => $pCardnumber,
                    'basic_fee' => $pBasicfee,
                    'charge_discount' => $pChargediscount,
                    'payment' => $pPayment,
                    'sid' => $pSid,
                    'created' => $tTime,
                    'id' => $tMData['id'],
                ); 
                if(!$tMIMO->update($tData)){
                    $tSMO->back();
                    Tool_Fnc::ajaxMsg('修改失败 002');
                }
            }
            //委托人信息
            $tFMMO = new FamilymembersModel;
            $tWdata = $tFMMO->field('id')->where('sid = '.$pSid.' and is_client = 1')->fRow();
            $tData = array(
                'name' => $pWtname,
                'relation' => $pWtrelation,
                'phone' => $pWtphone,
                'address' => $pWtaddress,
                'critcode' => $pWtcritcode,
                'is_client' => 1,
                'is_talk' => $pIstalk,
                'updated' => $tTime,
                'sid' => $pSid,
                'id' => $tWdata['id'],
            );
            if(!$tFMMO->update($tData)){
                $tSMO->back();
                Tool_Fnc::ajaxMsg('修改失败 003');
            }
            //家属信息
            foreach($tJs as $tJk=>$tJv){
                //$tFData = $tFMMO->field('id')->where('critcode = \''.$tJv['critcode'].'\'')->fRow();
                if(!empty($tJv['js_id'])){//记录存在修改操作
                    $tData = array(
                        'id' => $tJv['js_id'],
                        'name' => $tJv['name'],
                        'sex' => $tJv['sex'],
                        'relation' => $tJv['relation'],
                        'phone' => $tJv['phone'],
                        'critcode' => $tJv['critcode'],
                        'address' => $tJv['address'],
                        'is_client' => 2,
                        'sid' => $pSid,
                        'updated' => $tTime,
                        'status' => 1,
                    );
                    if(!$tFMMO->update($tData)){
                        Tool_Fnc::ajaxMsg('修改失败 004');
                    }
                }else{
                    if(!empty($tJv['name'])){
                        $tData = array(
                            'name' => $tJv['name'],
                            'sex' => $tJv['sex'],
                            'relation' => $tJv['relation'],
                            'critcode' => $tJv['critcode'],
                            'phone' => $tJv['phone'],
                            'address' => $tJv['address'],
                            'is_client' => 2,
                            'sid' => $pSid,
                            'status' => 1,
                            'created' => $tTime,
                        );
                        if(!$tFMMO->insert($tData)){
                            Tool_Fnc::ajaxMsg('修改失败 004');
                        }
                    }
                }
            }
            //扫描件信息
            if(!empty($_FILES['file_img'])){
                $fileimg = $_FILES['file_img'];
                $tImgurl = '/upload/scanning/'.date('Y').'/'.date('m').'/'.date('d').'/';
                $tDir = APPLICATION_PATH.'/public/upload/scanning/'.date('Y').'/'.date('m').'/'.date('d').'/';
                if($this->createDir($tDir)){
                    for($i=0;$i<count($fileimg['name']);$i++){
		                $tSaveName = $this->getSaveName();
                        if(strstr($fileimg['type'][$i],'image/jpeg')!==''){  
                            $tExt = 'jpg';  
                        }elseif(strstr($fileimg['type'][$i],'image/gif')!==''){  
                            $tExt ='gif';  
                        }elseif(strstr($fileimg['type'][$i],'image/png')!==''){  
 			                $tExt ='png';  
                        }else{
                            Tool_Fnc::ajaxMsg('非法文件');exit;
                        } 
                        $name = $fileimg['name'][$i];
                        $tImg = $tDir.$tSaveName.'.'.$tExt;
                        if(!move_uploaded_file($fileimg['tmp_name'][$i],iconv('UTF-8','gb2312',$tImg))){
                            Tool_Fnc::ajaxMsg('上传失败');exit;
                        };
                        //扫描件入库
                        $tData = array(
                            'sid' => $pSid,
                            'imgurl' => $tImgurl.$tSaveName.'.'.$tExt,
                            'created' => $tTime,
                        );
                        if(!$tScMO->insert($tData)){
                            Tool_Fnc::ajaxMsg('上传失败');exit;
                        }
                    }
                }
            }
            $tSMO->commit();
            Tool_Fnc::ajaxMsg('修改成功',1);
        }
        //编辑页面显示
        //$tSDatas = $tSMO->field('*')->where('id = '.$pSid)->fRow();
        $tSql = 'select s.*,mi.card_number card_number,mi.basic_fee basic_fee,mi.charge_discount charge_discount,mi.payment payment from '.$tSMO->table.' s left join '.$tMIMO->table.' mi on s.id = mi.sid where s.id = '.$pSid;
        $tSDatas = $tSMO->query($tSql)[0];
        //委托人信息
        $tFData = $tFMMO->field('is_talk,name,relation,phone,address,critcode')->where('sid = '.$pSid.' and is_client = 1')->fRow();
        $birthdate = '';
		for($i=0;$i<8;$i++){
			$birthdate .= $tSDatas['birth_date'][$i];
			if($i==3||$i==5){
				$birthdate .='-';
			}
		}
		$tSDatas['birth_date'] = $birthdate;
        $tSDatas['wt_name'] = $tFData['name'];
        $tSDatas['wt_relation'] = $tFData['relation'];
        $tSDatas['wt_phone'] = $tFData['phone'];
        $tSDatas['wt_address'] = $tFData['address'];
        $tSDatas['wt_critcode'] = $tFData['critcode'];
        $tSDatas['is_talk'] = $tFData['is_talk'];
        //家属信息
        $tFDatas = $tFMMO->field('*')->where('sid = '.$pSid.' and is_client = 2 and status = 1')->fList();
        //扫描件信息
        $tScDatas = $tScMO->field('id,imgurl')->where('sid = '.$pSid)->fList();
        //信息显示
        #民族显示
        $tNIMO = new NationinfoModel;
        $tNIDatas = $tNIMO->field('num,introduce')->fList();
        #宗教显示
        $tRIMO = new ReligioninfoModel;
        $tRIDatas = $tRIMO->field('num,introduce')->fList();
        #所在部门显示
        $tDIMO = new DepartmentinfoModel;
        $tDIDatas = $tDIMO->field('num,introduce')->fList();
        #文化程度显示
        $tEIMO = new EducationlevelsinfoModel;
        $tEIDatas = $tEIMO->field('num,introduce')->fList();
        #身体状态显示
        $tBIMO = new BodystatusinfoModel;
        $tBIDatas = $tBIMO->field('num,introduce')->fList();
        //支付方式显示
        $tPIMO = new PaymentinfoModel;
        $tPIDatas = $tPIMO->field('num,introduce')->fList();
        //收费折扣显示
        $tDIMO = new DiscountinfoModel;
        $tDisDatas = $tDIMO->field('num,introduce')->fList();
        //性别显示
        $tSIMO = new SexinfoModel;
        $tSIDatas = $tSIMO->field('*')->fList();

        $this->assign('tId',$pSid);
        $this->assign('tSIDatas',$tSIDatas);
        $this->assign('tSDatas',$tSDatas);
        $this->assign('tFDatas',$tFDatas);
        $this->assign('tNIDatas',$tNIDatas);
        $this->assign('tRIDatas',$tRIDatas);
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tEIDatas',$tEIDatas);
        $this->assign('tBIDatas',$tBIDatas);
        $this->assign('tPIDatas',$tPIDatas);
        $this->assign('tDisDatas',$tDisDatas);
        $this->assign('tScDatas',$tScDatas);

    }
    public function editsupportworkerAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);

        $tAMO = new AdminModel;
        $tWMO = new WorkerModel;
        $tWSMO = new WorkerseniorModel;
        $tDIMO = new DepartmentinfoModel;
        $tSMO = new SeniorModel;
        $tDepartment = $tSMO->field('department')->where('id = '.$pId)->fRow();

        $tWhere = ' 1 and a.role = 7 and w.department = '.$tDepartment['department'];
        //$tCount = $tAMO->field('count(*) c')->where($tWhere)->fList();
        $tSql = 'select count(*) c from '.$tAMO->table.' a left join '.$tWMO->table.' w on a.w_id = w.id where '.$tWhere;
        $tCount = $tAMO->query($tSql);

        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        $tDatas = array();
        if(!empty($tCnt)){
            $tSql = 'select w.id w_id,w.cases cases,w.skill skill,di.introduce department,a.id id,w.name name from '.$tAMO->table.' a left join '.$tWMO->table.' w on a.w_id = w.id left join '.$tDIMO->table.' di on w.department = di.num where '.$tWhere.' order by a.id desc limit '.$tPage->limit();
            $tDatas = $tAMO->query($tSql);
            foreach($tDatas as $tKey=>$tValue){
                $tWCount = $tWSMO->field('count(id) c')->where('sw_id = '.$tValue['id'])->fList();
                $tSql = 'select sid,sw_id from '.$tWSMO->table.' where sid='.$pId.' and sw_id='.$tValue['id'];
                $tBang = $tWSMO->query($tSql);
                $tBD = empty($tBang)?0:1;
                $tWCnt = empty($tWCount[0]['c'])?0:$tWCount[0]['c'];
                $tDatas[$tKey]['w_cnt'] = $tWCnt;
                $tDatas[$tKey]['tBD'] = $tBD;
            }
            //$tDatas = $tAMO->field('*')->where($tWhere)->order(' id desc')->limit($tPage->limit())->fList();
            foreach($tDatas as $tKey=>$tValue){
                $tCases = explode(',',$tValue['cases']);
                foreach($tCases as $tK=>$tV){
                    switch($tV){
                        case '1':$tCases[$tK] = '脑中风';break;
                        case '2':$tCases[$tK] = '老年痴呆';break;
                        case '3':$tCases[$tK] = '插尿管';break;
                        case '4':$tCases[$tK] = '鼻伺';break;
                        case '5':$tCases[$tK] = '骨折';break;
                        case '6':$tCases[$tK] = '肿瘤';break;
                        case '7':$tCases[$tK] = '腰间盘突出';break;
                        case '8':$tCases[$tK] = '自理';break;
                        case '9':$tCases[$tK] = '半自理';break;
                        case '10':$tCases[$tK] = '不自理';break;
                        case '11':$tCases[$tK] = '精神病';break;
                        default:$tCases[$tK] = '无';
                    }
                }
                $tCases = implode(',',$tCases);
                $tDatas[$tKey]['cases'] = $tCases;
                $tSkill = explode(',',$tValue['skill']);
                foreach($tSkill as $tK1=>$tV1){
                    switch($tV1){
                        case '1':$tSkill[$tK1] = '做饭';break;
                        case '2':$tSkill[$tK1] = '给病人翻身';break;
                        case '3':$tSkill[$tK1] = '拍痰';break;
                        case '4':$tSkill[$tK1] = '吸氧';break;
                        case '5':$tSkill[$tK1] = '测血压';break;
                        case '6':$tSkill[$tK1] = '测血糖';break;
                        default:$tSkill[$tK1] = '无';
                    }
                }
                $tSkill = implode(',',$tSkill);
                $tDatas[$tKey]['skill'] = $tSkill;
            }
        }
        $this->assign('pId',$pId);
        $this->assign('tDatas',$tDatas);
    }
    public function boundAction(){
        $p = $_REQUEST;
        $pSwID = empty($p['sw_id'])?Tool_Fnc::ajaxMsg('sw_id不能为空'):Tool_Fnc::safe_string($p['sw_id']);
        $pSid = empty($p['sid'])?Tool_Fnc::ajaxMsg('sid不能为空'):Tool_Fnc::safe_string($p['sid']);

        $tWSMO = new WorkerseniorModel;
        $tWRow = $tWSMO->field('id')->where('sid = '.$pSid)->fRow();

        $tTime = time();
        if(empty($tWRow)){//记录为空添加操作
            $tData = array(
                'sid' => $pSid,
                'sw_id' => $pSwID,
                'created' => $tTime,
            );
            if(!$tId = $tWSMO->insert($tData)){
                Tool_Fnc::ajaxMsg('绑定失败');
            }
        }else{//记录不为空更新操作
            $tData = array(
                'sid' => $pSid,
                'sw_id' => $pSwID,
                'updated' => $tTime,
                'id' => $tWRow['id'],
            );
            if(!$tWSMO->update($tData)){
                Tool_Fnc::ajaxMsg('绑定失败');
            }
        }
        Tool_Fnc::ajaxMsg('绑定成功',1);
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);
        $pPid = empty($p['pid'])?'':Tool_Fnc::safe_string($p['pid']);
        $pFid = empty($p['fid'])?'':Tool_Fnc::safe_string($p['fid']);
        $pScID = empty($p['sc_id'])?'':Tool_Fnc::safe_string($p['sc_id']);

        $tSMO = new SeniorModel;
        $tWSMO = new WorkerseniorModel;

        if(!empty($pId)){
            $tSMO->begin();
            $tSql = 'update '.$tSMO->table.' set status = 2 where id = '.$pId;
            if(!$tSMO->exec($tSql)){
                Tool_Fnc::ajaxMsg('删除失败 001');
            }
            $tWSData = $tWSMO->field('id')->where('sid = '.$pId)->fRow();
            if(!empty($tWSData['id'])){
                $tSql = 'delete from '.$tWSMO->table.' where sid = '.$pId;
                if(!$tWSMO->exec($tSql)){
                    $tSMO->back();
                    Tool_Fnc::ajaxMsg('删除失败 002');
                }
            }
            $tSMO->commit();
            Tool_Fnc::ajaxMsg('删除成功',1);
        }
        if(!empty($pPid)){
            $tPMO = new PlanModel;
            $tData = array(
                'is_delete' => 2,
                'id' => $pPid,
                'updated' => time(),
            );
            if(!$tPMO->update($tData)){
                Tool_Fnc::ajaxMsg('删除失败 003'); 
            }
            Tool_Fnc::ajaxMsg('删除成功',1);
        }
        if(!empty($pFid)){
            $tFMMO = new FamilymembersModel;
            $tData = array(
                'status' => 2,
                'id' => $pFid,
                'updated' => time(),
            );
            if(!$tFMMO->update($tData)){
                Tool_Fnc::ajaxMsg('删除失败 004'); 
            }
            Tool_Fnc::ajaxMsg('删除成功001',1);
        }
        if(!empty($pScID)){
            $tScMO = new ScanningModel; 
            if(!$tScMO->del($pScID)){
                Tool_Fnc::ajaxMsg('删除失败 005');
            }
            Tool_Fnc::ajaxMsg('删除成功',1);
        }
    }
    public function planAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);
        
        $tPMO = new PlanModel;
        $tEIMO = new EventinfoModel;
        
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pId = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);
            $pAt = empty($p['at'])?'':Tool_Fnc::safe_string($p['at']);
            $pStartdate = empty($p['startdate'])?'':$p['startdate'];
            $pEnddate = empty($p['enddate'])?'':$p['enddate'];
            $pRespondtime = empty($p['respondtime'])?'':$p['respondtime'];
            $pRemindtype = empty($p['remind_type'])?'':$p['remind_type'];
            $pDescriptions = empty($p['descriptions'])?'':$p['descriptions'];
            $pStatus = empty($p['status'])?'':$p['status'];
            $pIsshow = empty($p['is_show'])?'':$p['is_show'];
            $pPiID = empty($p['pi_id'])?'':$p['pi_id'];
            $pSpecial = empty($p['special'])?'':$p['special'];
            $pPid = empty($p['pid'])?'':$p['pid'];


            $tNewarr = array();

            $tTime = time();

            $tDate = date('Y-m-d',$tTime);
            $tWSMO = new WorkerseniorModel;
            $tWSData = $tWSMO->field('sw_id')->where('sid = '.$pId)->fRow();

            $tArr = array();
            foreach($p as $tKey=>$tValue){
                if(is_numeric($tKey)){
                    array_push($tArr,$tKey);
                } 
            }
            $tPlanarr = array(array());
            if($pSpecial == 1){
                for($i=1;$i<count($pRespondtime);$i++){
                    for($j=0;$i<count($pRespondtime)-$i;$j++){
                        if(abs(strtotime($tDate.' '.$pRespondtime[$i])-strtotime($tDate.' '.$pRespondtime[$i+1])) < 30*60*60){
                            echo "<javascript>alert('特殊事件响应时间间隔不能少于30分钟')</javascript>";exit;        
                        } 
                    }
                }
                $pEventtype = empty($p['event_type'])?'':$p['event_type'];
                for($i=0;$i<count($pEventtype);$i++){
                    $tPlanarr[$i]['plan_type'] = $pRemindtype[$i];
                    $tPlanarr[$i]['event_type'] = $pEventtype[$i];
                    $tPlanarr[$i]['sw_id'] = $tWSData[$i];
                    $tPlanarr[$i]['status'] = $pStatus[$i];
                    $tPlanarr[$i]['startdate'] = $pStartdate[$i];
                    $tPlanarr[$i]['enddate'] = $pEnddate[$i];
                    $tPlanarr[$i]['starttime'] = strtotime($pStartdate[$i].' '.$pRespondtime[$i]);
                    $tPlanarr[$i]['endtime'] = strtotime($pEnddate[$i].' '.$pRespondtime[$i]);
                    $tPlanarr[$i]['respond_time'] = $pRespondtime[$i];
                    $tPlanarr[$i]['description'] = $pDescriptions[$i];
                    $tPlanarr[$i]['created'] = $tTime;
                    $tPlanarr[$i]['pi_id'] = $pPiID[$i];
                    $tPlanarr[$i]['pid'] = $pPid[$i];
                }
                foreach($tPlanarr as $tRow){
                    //判断同一类型的事件30分钟内是否存在 
                    if(empty($tRow['respond_time'])){Tool_Fnc::ajaxMsg('特殊事件响应时间有的为空，请填写完整!',0);exit;}
                    $tPData = $tPMO->field('respond_time')->where('event_type = '.$tRow['event_type'].' and sid = '.$pId)->fRow();
                    $tEData = $tEIMO->field('introduce')->where('num = '.$tRow['event_type'])->fRow();

                    if(!empty($tPData['respond_time'])){
                        if(abs(strtotime($tDate.' '.$tRow['respond_time']) - strtotime($tDate.' '.$tPData['respond_time'])) < 30*60){Tool_Fnc::ajaxMsg('30分钟内已存在'.$tEData['introduce'].'事件,请稍等再添加!',0);exit;} 
                    }
                    if(!empty($tRow['pid'])){
                        $tData = array(
                            'sid' => $pId,
                            'plan_type' => $tRow['plan_type'],
                            'event_type' => $tRow['event_type'],
                            'sw_id' => $tRow['sw_id'],
                            'status' => $tRow['status'],
                            'startdate' => $tRow['startdate'],
                            'enddate' => $tRow['enddate'],
                            'starttime' => $tRow['starttime'],
                            'endtime' => $tRow['endtime'],
                            'respond_time' => $tRow['respond_time'],
                            'description' => $tRow['description'],
                            'updated' => $tTime,
                            'is_delete' => 1,
                            'id' => $tRow['pid'],
                        );
                        if(!$tPMO->update($tData)){
                            Tool_Fnc::ajaxMsg('更新失败');
                        }
                    } 
                    $tData = array(
                        'sid' => $pId,
                        'plan_type' => $tRow['plan_type'],
                        'event_type' => $tRow['event_type'],
                        'sw_id' => $tRow['sw_id'],
                        'status' => $tRow['status'],
                        'startdate' => $tRow['startdate'],
                        'enddate' => $tRow['enddate'],
                        'starttime' => $tRow['starttime'],
                        'endtime' => $tRow['endtime'],
                        'respond_time' => $tRow['respond_time'],
                        'description' => $tRow['description'],
                        'is_delete' => 1,
                        'created' => $tTime,
                    );
                    if(!$tId = $tPMO->insert($tData)){
                        Tool_Fnc::ajaxMsg('添加失败 001',0);
                    }
                }
            }
            for($i=0;$i<count($pStartdate);$i++){
                $tPlanarr[$i]['sid'] = $pId;
                $tPlanarr[$i]['plan_type'] = $pRemindtype[$i];
                $tPlanarr[$i]['event_type'] = $tArr[$i];
                $tPlanarr[$i]['sw_id'] = $tWSData['sw_id'];
                $tPlanarr[$i]['status'] = $pStatus[$i];
                $tPlanarr[$i]['startdate'] = $pStartdate[$i];
                $tPlanarr[$i]['enddate'] = $pEnddate[$i];
                $tPlanarr[$i]['starttime'] = strtotime($pStartdate[$i].' '.$pRespondtime[$i]);
                $tPlanarr[$i]['endtime'] = strtotime($pEnddate[$i].' '.$pRespondtime[$i]);
                $tPlanarr[$i]['respond_time'] = $pRespondtime[$i];
                $tPlanarr[$i]['description'] = $pDescriptions[$i];
                $tPlanarr[$i]['created'] = $tTime;
                $tPlanarr[$i]['at'] = $pAt;
                $tPlanarr[$i]['is_show'] = $pIsshow[$i];
                $tPlanarr[$i]['pi_id'] = $pPiID[$i];
                $tPlanarr[$i]['pid'] = $pPiID[$i];
            }
            foreach($tPlanarr as $tRow){
                $tPData = $tPMO->field('id')->where('event_type = '.$tRow['event_type'].' and at = '.$tRow['at'].' and sid = '.$tRow['sid'])->fRow();    
                if(empty($tPData['id'])){
                    $tData = array(
                        'sid' => $tRow['sid'],
                        'plan_type' => $tRow['plan_type'],
                        'event_type' => $tRow['event_type'],
                        'sw_id' => $tRow['sw_id'],
                        'status' => $tRow['status'],
                        'startdate' => $tRow['startdate'],
                        'enddate' => $tRow['enddate'],
                        'starttime' => $tRow['starttime'],
                        'endtime' => $tRow['endtime'],
                        'respond_time' => $tRow['respond_time'],
                        'description' => $tRow['description'],
                        'is_show' => $tRow['is_show'],
                        'pi_id' => $tRow['pi_id'],
                        'at' => $tRow['at'],
                        'is_delete' => 1,
                        'created' => $tTime,
                    );
                    if(!$tId = $tPMO->insert($tData)){
                        Tool_Fnc::ajaxMsg('添加失败');
                    }
                }else{
                    $tData = array(
                        'sid' => $tRow['sid'],
                        'plan_type' => $tRow['plan_type'],
                        'event_type' => $tRow['event_type'],
                        'sw_id' => $tRow['sw_id'],
                        'status' => $tRow['status'],
                        'startdate' => $tRow['startdate'],
                        'enddate' => $tRow['enddate'],
                        'starttime' => $tRow['starttime'],
                        'endtime' => $tRow['endtime'],
                        'respond_time' => $tRow['respond_time'],
                        'description' => $tRow['description'],
                        'is_show' => $tRow['is_show'],
                        'pi_id' => $tRow['pi_id'],
                        'at' => $tRow['at'],
                        'is_delete' => 1,
                        'updated' => $tTime,
                        'id' => $tPData['id'],
                    );
                    if(!$tPMO->update($tData)){
                        Tool_Fnc::ajaxMsg('修改失败');
                    }
                }
            }
            Tool_Fnc::ajaxMsg('修改成功',1);
        }
        
        $tPIMO = new PlaninfoModel;
        $tEIMO = new EventinfoModel;
        //页面显示
        #早上
        //$tPIDatas = $tPIMO->field('*')->where('at = 1')->fList();
        $tSql = 'select p.pi_id pi_id,p.is_show is_show,p.event_type event_type,pi.*,ei.num num,p.status status,p.startdate startdate,p.enddate enddate,p.respond_time respond_time,p.plan_type plan_type,p.description description from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name left join '.$tPMO->table.' p on p.pi_id = pi.id where pi.at = 1 and pi.event_type <> 9 and sid = '.$pId;
        //$tSql = 'select pi.name name,pi.descriptions descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 1';
        $tPIDatas = $tPIMO->query($tSql);
        if(empty($tPIDatas)){
            $tSql = 'select pi.id pi_id,pi.name name,pi.descriptions descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 1 and pi.event_type <> 9';
            $tPIDatas = $tPIMO->query($tSql);
        }
        #上午
        //$tSql = 'select pi.*,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 2';
        //$tSql = 'select pi.name name,pi.descriptions descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 2';
        //$tPIDatas1 = $tPIMO->query($tSql);
        $tSql = 'select p.pi_id pi_id,p.is_show is_show,p.event_type event_type,pi.*,ei.num num,p.status status,p.startdate startdate,p.enddate enddate,p.respond_time respond_time,p.plan_type plan_type,p.description description from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name left join '.$tPMO->table.' p on p.pi_id = pi.id where pi.at = 2 and pi.event_type <> 9 and sid = '.$pId;
        $tPIDatas1 = $tPIMO->query($tSql);
        if(empty($tPIDatas1)){
            $tSql = 'select pi.id pi_id,pi.name name,pi.descriptions descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 2 and pi.event_type <> 9';
            $tPIDatas1 = $tPIMO->query($tSql);
        }
        //$tPIDatas1 = $tPIMO->field('*')->where('at = 2')->fList();
        #中午
        //$tSql = 'select pi.*,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 3';
        //$tPIDatas2 = $tPIMO->query($tSql);
        //$tSql = 'select pi.name name,pi.descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 3';
        $tSql = 'select p.pi_id pi_id,p.is_show is_show,p.event_type event_type,pi.*,ei.num num,p.status status,p.startdate startdate,p.enddate enddate,p.respond_time respond_time,p.plan_type plan_type,p.description description from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name left join '.$tPMO->table.' p on p.pi_id = pi.id where pi.at = 3 and pi.event_type <> 9 and sid = '.$pId;
        $tPIDatas2 = $tPIMO->query($tSql);
        if(empty($tPIDatas2)){
            $tSql = 'select pi.id pi_id,pi.name name,pi.descriptions descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 3 and pi.event_type <> 9';
            $tPIDatas2 = $tPIMO->query($tSql);
        }
        //$tPIDatas2 = $tPIMO->field('*')->where('at = 3')->fList();
        #下午
        //$tSql = 'select pi.*,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 4';
        //$tPIDatas3 = $tPIMO->query($tSql);
        //$tSql = 'select pi.name name,pi.descriptions descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 4';
        $tSql = 'select p.pi_id pi_id,p.is_show is_show,p.event_type event_type,pi.*,ei.num num,p.status status,p.startdate startdate,p.enddate enddate,p.respond_time respond_time,p.plan_type plan_type,p.description description from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name left join '.$tPMO->table.' p on p.pi_id = pi.id where pi.at = 4 and pi.event_type <> 9 and sid = '.$pId;
        $tPIDatas3 = $tPIMO->query($tSql);
        if(empty($tPIDatas3)){
            $tSql = 'select pi.id pi_id,pi.name name,pi.descriptions descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 4 and pi.event_type <> 9';
            $tPIDatas3 = $tPIMO->query($tSql);
        }
        //$tPIDatas3 = $tPIMO->field('*')->where('at = 4')->fList();
        #晚上
        //$tSql = 'select pi.*,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 1';
        //$tPIDatas4 = $tPIMO->query($tSql);
        //$tSql = 'select pi.name anme,pi.descriptions descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 5';
        $tSql = 'select p.pi_id pi_id,p.is_show is_show,p.event_type event_type,pi.*,ei.num num,p.status status,p.startdate startdate,p.enddate enddate,p.respond_time respond_time,p.plan_type plan_type,p.description description from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name left join '.$tPMO->table.' p on p.pi_id = pi.id where pi.at = 5 and pi.event_type <> 9 and sid = '.$pId;
        $tPIDatas4 = $tPIMO->query($tSql);
        if(empty($tPIDatas4)){
            $tSql = 'select pi.id pi_id,pi.name name,pi.descriptions descriptions,ei.num num from '.$tPIMO->table.' pi left join '.$tEIMO->table.' ei on ei.introduce = pi.name where pi.at = 5 and pi.event_type <> 9';
            $tPIDatas4 = $tPIMO->query($tSql);
        }
        //$tPIDatas4 = $tPIMO->field('*')->where('at = 5')->fList();
        //喝水
        $tSql = 'select p.id pid,p.event_type event_type,p.status status,p.startdate startdate,p.enddate enddate,p.respond_time respond_time,p.plan_type plan_type,p.description description from '.$tPMO->table.' p where p.event_type = 9 and p.is_delete = 1 and sid = '.$pId;
        $tPIDatas5 = $tPIMO->query($tSql);
        //翻身
        $tSql = 'select p.id pid,p.event_type event_type,p.status status,p.startdate startdate,p.enddate enddate,p.respond_time respond_time,p.plan_type plan_type,p.description description from '.$tPMO->table.' p where p.event_type = 23 and p.is_delete = 1 and sid = '.$pId;
        $tPIDatas6 = $tPIMO->query($tSql);
        
        $this->assign('pId',$pId);
        $this->assign('tPIDatas',$tPIDatas);
        $this->assign('tPIDatas1',$tPIDatas1);
        $this->assign('tPIDatas2',$tPIDatas2);
        $this->assign('tPIDatas3',$tPIDatas3);
        $this->assign('tPIDatas4',$tPIDatas4);
        $this->assign('tPIDatas5',$tPIDatas5);
        $this->assign('tPIDatas6',$tPIDatas6);
    }
	public function createDir($folder){
		$reval = false;
		if(!file_exists($folder)){
			@umask(0);
			preg_match_all('/([^\/]*)\/?/i', $folder, $atmp);
			$base = ($atmp[0][0] == '/') ? '/' : '';
			
			foreach($atmp[1] AS $val) {
				if('' != $val){
					$base .= $val;
					if ('..' == $val || '.' == $val) {
						$base .= '/';
						continue;
					}
				}else{
					continue;
				}

				$base .= '/';

				if(!file_exists($base)){
					if(@mkdir($base, 0777)){
						@chmod($base, 0777);
						$reval = true;
					}
				}
			}
		}else{
			$reval = is_dir($folder);
		}
		
		clearstatcache();
		return $reval;
	}
	public function getSaveName(){
		$newName = md5(md5(date('YmdHis') . rand(1000, 99999)).'pyp123');

		return $newName;
	}
    
}
?>
